﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WcsZz.Extend;
using wms_wpf.Apiservice;
using wms_wpf.DialogView;
using wms_wpf.Dto;
using wms_wpf.InPut;

namespace wms_wpf.DialogViewModel
{
    public class CheckItem : BindableBase
    {
        private string? itemid;

        public string? ItemId
        {
            get { return itemid; }
            set { itemid = value;RaisePropertyChanged(); }
        }


        private bool? isselect;

        public bool? IsSelect
        {
            get { return isselect; }
            set { isselect = value;RaisePropertyChanged(); }
        }

    }
    public class DetailsDialogViewModel : BindableBase, IDialogAware
    {
        string Machine = Environment.GetEnvironmentVariable("Machine", EnvironmentVariableTarget.Machine);
        private IBaseService baseService;
        private readonly DetailsDialog detailsDialogView;
        public DetailsDialogViewModel(DetailsDialog detailsDialogView) 
        {
            this.detailsDialogView = detailsDialogView;
            ItemIds =new ObservableCollection<CheckItem>();
            OrderNumbers=new ObservableCollection<string>();
            baseService = new BaseService();
            SelectCommand = new DelegateCommand<string>(Select);
            PreviewCommand = new DelegateCommand<string>(PreView);
        }





        public string Title { get; set; }
 
        private string content;
        public string Content
        {
            get { return content; }
            set { content = value; }
        }

  
        private List<NeetOutMaterialsDto> neetOutMaterialsDtos;
        public List<NeetOutMaterialsDto> NeetOutMaterialsDtos
        {
            get { return neetOutMaterialsDtos; }
            set { neetOutMaterialsDtos = value; RaisePropertyChanged(); }
        }

        private List<NeetOutMaterialsDto> neetOutMaterialsDtoviews;
        public List<NeetOutMaterialsDto> NeetOutMaterialsDtoviews
        {
            get { return neetOutMaterialsDtoviews; }
            set { neetOutMaterialsDtoviews = value; RaisePropertyChanged(); }
        }

        public event Action<IDialogResult> RequestClose;
        public ObservableCollection<CheckItem> ItemIds { get; set; }


       

        public ObservableCollection<string> OrderNumbers { get; set; }
        public DelegateCommand<string> SelectCommand { get; set; }
        public DelegateCommand<string> PreviewCommand { get; set; }
        public bool CanCloseDialog()
        {
          return  true;
        }

        public void OnDialogClosed()
        {

           
            EventAggregatorHelper.EventAggregator.SendMessage("true", "MainWindow");//"Setting" 给消息聚合器打上标记 
        }

        public async void OnDialogOpened(IDialogParameters parameters)//页面跳转过来
        {
            EventAggregatorHelper.EventAggregator.SendMessage("false", "MainWindow");
            List<int> keys = new List<int>() {1,2,3,4,5,6,7,8 }; 
            List<string> NeetOutMaterialsGuidId = new List<string>();
            InPut_BaseService21 inPut = new InPut_BaseService21();
            inPut.IsEnable = false;
            inPut.Machine = Machine;
           
            foreach (var key in keys)
            {
                string var = parameters.GetValue<string>(key.ToString());
                if (string.IsNullOrEmpty(var))
                {
                    continue;
                }
                ItemIds. Add(new CheckItem { IsSelect=true,ItemId=var});


                inPut.hSerials.Add(var);

            }
           await Task.Delay(100);
            await CreateTaskManyNeetOutNewAsync(inPut);
        }

        private async void PreView(string obj)//预览/或真实
        {
            InPut_BaseService21 inPut = new InPut_BaseService21();
            if(bool.Parse( obj))
            {
                inPut.IsEnable=true;
            }
            else
            {
                inPut.IsEnable=false;
            }
          
            inPut.Machine = Machine;
            foreach (CheckItem checkBox in detailsDialogView.checkBoxList.Items)
            {
                if ((bool)checkBox.IsSelect)
                {
                    inPut.hSerials.Add(checkBox.ItemId);
                }
            }
            await CreateTaskManyNeetOutNewAsync(inPut);

            if(inPut.IsEnable == true)
            {
                RequestClose.Invoke(new DialogResult(ButtonResult.OK));
            }
             

        }


        private async Task CreateTaskManyNeetOutNewAsync(InPut_BaseService21 inPut)
        {
            try
            {
                NeetOutMaterialsDtos = await baseService.CreateTaskManyNeetOutNew(inPut);
                if (NeetOutMaterialsDtos == null)
                {
                   
                    return;
                }
                NeetOutMaterialsDtoviews = NeetOutMaterialsDtos;
                if (NeetOutMaterialsDtos.Count > 0)
                {
                    OrderNumbers.Clear();
                    OrderNumbers.AddRange(new ObservableCollection<string>(NeetOutMaterialsDtos.DistinctBy(x => x.OrderNo).Select(x => x.OrderNo)));

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message+"mahcine:"+inPut.Machine+"hserials:"+inPut.hSerials+"enable:"+inPut.IsEnable);
            }
        }

        private void Select(string obj)
        {
            
            NeetOutMaterialsDtoviews = NeetOutMaterialsDtos.Where(x=>x.OrderNo == obj).ToList();
        }
    }
}
